package com.zwface.demo.http;

import android.content.Context;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.zwface.demo.App;
import com.zwface.demo.utils.FastjsonHelper;
import com.zwface.demo.utils.SharedPreferenceUtils;

import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;


public class ZWAsyncHttpClient
{
    private static final String BASE_URL = "";

    private static AsyncHttpClient m_client;


    public static void get(String url, Map<String,?> map, HttpCallback callback)
    {
        RequestParams params = setParams((Map<String, Object>) map);
        String paramStr = FastjsonHelper.serialize(map);
        ZwAsyncHttpResponseHandler responseHandler = new ZwAsyncHttpResponseHandler(callback,url,paramStr);
        getInstance(App.getInstance().getApplicationContext(),url).get(ActivityUtils.getTopActivity(),getAbsoluteUrl(url),params, responseHandler);
    }

    public static void post(String url, Map<String,?> map, HttpCallback callback)
    {
        String paramStr = FastjsonHelper.serialize(map);
        //RequestParams params = setParams((Map<String, Object>) map);
        ZwAsyncHttpResponseHandler responseHandler = new ZwAsyncHttpResponseHandler(callback,url,paramStr);
        getInstance(App.getInstance().getApplicationContext(),url).post(ActivityUtils.getTopActivity(),getAbsoluteUrl(url),getEntity(url,paramStr), "application/json", responseHandler);
    }

    private static RequestParams setParams(Map<String, Object> map)
    {
        if( map == null )
            map = new HashMap<>();

        RequestParams params = new RequestParams();
        for (Map.Entry<String, Object> entry : map.entrySet())
            params.put(entry.getKey(),entry.getValue());
        params.setContentEncoding("UTF-8");
        return params;
    }

    public static AsyncHttpClient getInstance(Context paramContext, String url)
    {
        if ( m_client == null )
        {
            m_client = new AsyncHttpClient();
            PersistentCookieStore ck = new PersistentCookieStore(paramContext);
            m_client.setCookieStore(ck);
        }

        String token = SharedPreferenceUtils.getParam(paramContext,SharedPreferenceUtils.ACCESS_TOKEN);

        m_client.removeAllHeaders();
        if(url.indexOf("/p/") != -1 && !StringUtils.isEmpty(token))
        {
            m_client.addHeader("Authorization","Bearer "+token);
            LogUtils.d("网络请求--url---\n"+url+"--token--"+token);
        }
        //m_client.addHeader(HTTP.CONTENT_TYPE,"application/json");
        return m_client;
    }

    private static ByteArrayEntity getEntity(String url, String paramStr)
    {
        ByteArrayEntity entity = null;
        try {
            entity = new ByteArrayEntity(paramStr.getBytes("UTF-8"));
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return entity;
    }

    private static String getAbsoluteUrl(String relativeUrl)
    {
        String url = "";
        url = BASE_URL + relativeUrl;
        return url;
    }


}